package com.hbue.water.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author yushu
 * @date 2021/12/29
 * @apiNate
 * SpringBoot解决跨域问题
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter createCorsFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 设置允许的网站域名，* 表示全部允许
        corsConfiguration.addAllowedOrigin("*");
        // 设置允许的请求方式，* 表示全部允许
        corsConfiguration.addAllowedMethod("*");
        // 设置允许的请求头，* 表示全部允许
        corsConfiguration.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 匹配所有请求
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(source);
    }
}
